package com.tianxiabuyi.model.task;

import com.alibaba.fastjson.TypeReference;
import com.tianxiabuyi.model.pojo.PhysicalCheck;
import com.tianxiabuyi.persist.DBTools;
import com.tianxiabuyi.persist.QBuilder;
import com.tianxiabuyi.persist.Where;
import com.tianxiabuyi.utils.HttpUtil;
import com.tianxiabuyi.utils.JSONUtil;
import org.apache.http.protocol.HTTP;

import java.util.*;

/**
 * 体检数据抓取任务类
 * 定义的属性可能不够，所以如果后期有新的医院可以同步数据，并且参数不止如下几个，如果不想增加属性可以在调用构造方法时除了本来的属性，另外添加动态可变参数，并且使用params.get(i) 方式获取
 * Created by yanghaixiao on 2017/6/12.
 */
public class PhysicalCheckTask extends AbstractTask {
    private String name;     // 患者姓名
    private String cardNumber;  // 身份证号
    private String checkNumber; // 检查单号（体检编号等等）
    private Integer hospital;   // 同步数据的医院 0表示全部合作医院

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCheckNumber() {
        return checkNumber;
    }

    public void setCheckNumber(String checkNumber) {
        this.checkNumber = checkNumber;
    }

    public Integer getHospital() {
        return hospital;
    }

    public void setHospital(Integer hospital) {
        this.hospital = hospital;
    }

    public PhysicalCheckTask(String name, String cardNumber, String checkNumber, Integer hospital, Object... parameters) {
        super(parameters);
        this.name = name;
        this.cardNumber = cardNumber;
        this.checkNumber = checkNumber;
        this.hospital = hospital;
    }

    @Override
    public void run() {
        switch (hospital) {
            // hospital为0时用于根据身份证号到各合作医院抓取数据，一般是在完成初次实名认证后使用，以及后期的定时同步任务
            case 0:{
                xcrmyy(checkNumber, name, cardNumber);
                smh(checkNumber, cardNumber);
            }
            break;
            case 1002:{
                sdfey(cardNumber);
            }
            break;
            case 1010: {
                smh(checkNumber, cardNumber);
            }
            break;
            case 1014: {
                xcrmyy(checkNumber, name, cardNumber);
            }
            break;
            default:{
                System.out.println(String.format("暂时不支持该医院(%s)体检数据的同步", new Object[]{hospital}));
            };
            break;
        }
    }


    /**
     * 相城人民医院体检数据
     * @param checkNumber 201503020006
     * @param name 吴可可
     * @param cardNumber 411481198407184256
     * @return
     */
   void xcrmyy(String checkNumber, String name, String cardNumber) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("tjbh", checkNumber);
        map.put("name", name);
        map.put("sfzh", cardNumber);
        String json = HttpUtil.post("http://192.168.1.203:8080/api/physical_check_transfer.jsp", map);
        List<PhysicalCheck> list = JSONUtil.fromJson(json, new TypeReference<List<PhysicalCheck>>(){});
        if (list == null)
            return;
        List<String> checkNumberList = QBuilder.db("basedb").select("checkNumber").from("PhysicalCheck").where(new Where("hospital=", 1014)).query().getResultObj();
        // 如果体检编号已存在，跳过不再存储
        Iterator<PhysicalCheck> it = list.iterator();
        while (it.hasNext()) {
            PhysicalCheck pc = it.next();
            pc.setIdenditiyNumber(cardNumber);
            if (checkNumberList.contains(pc.getCheckNumber())) {
                it.remove();
            }
        }
        if (list.size() != 0) {
            DBTools.db("basedb").addItem(list);
        }
    }

    /**
     * 苏州市立医院体检数据
     * @param checkNumber
     * @param cardNumber
     */
    void smh(String checkNumber, String cardNumber) {
        QBuilder.db("basedb").execHQL("update PhysicalCheck set cardNumber='" + cardNumber + "' where hospital=1010 and checkNumber='" + checkNumber + "'");
    }

    void sdfey(String cardNumber) {
        String json = HttpUtil.get("http://221.224.34.163:7071/patient/phsical_check/physical_check_transfer.jsp?sfzh=" + cardNumber);
        System.out.println(json);
        List<Map> list = JSONUtil.fromJson(json, new TypeReference<List<Map>>(){});
        List<PhysicalCheck> reports = new ArrayList<>();
        List<PhysicalCheck> parents = new ArrayList<>();
        Set<String> set = new HashSet<>();
        Map<String, Integer> idMap = new HashMap<>();
        for (Map map:list) {
            PhysicalCheck pc = PhysicalCheck.fromMap(map, PhysicalCheck.class);
            pc.setHospital(1002);
            String parentItemName = map.get("parent_item_name") + "";
            String parentItemId = map.get("parent_item_id") + "";
            if (!set.contains(parentItemName)) {
                set.add(parentItemName);
                PhysicalCheck pc2 = PhysicalCheck.fromMap(map, PhysicalCheck.class);
                pc2.setItemId(parentItemId);
                pc2.setItemName(parentItemName);
                pc2.setHospital(1002);
                Integer id = (Integer) DBTools.db("basedb").addItem(pc2);
                idMap.put(parentItemName, id);
            }
            pc.setParentId(idMap.get(parentItemName) + "");
            reports.add(pc);
        }
        if (reports.size() != 0)
            DBTools.db("basedb").addItem(reports);
    }
}
